package org.rain.common.classmeta;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * created by yangtong on 2025/4/15 16:54:24
 * <br/>
 * 字段元数据
 */
public class FieldMetadata {
    private final Field field;
    private final Map<Class<? extends Annotation>, Annotation> annotations;

    public FieldMetadata(Field field) {
        this.field = field;
        this.annotations = Arrays.stream(field.getAnnotations())
                .collect(Collectors.toMap(Annotation::annotationType, a -> a));
    }

    public <A extends Annotation> A getAnnotation(Class<A> type) {
        return (A) annotations.get(type);
    }

    public boolean hasAnnotation(Class<? extends Annotation> type) {
        return annotations.containsKey(type);
    }
}
